package com.dlc.shop.seckill.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dlc.shop.bean.model.OrderItem;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.service.OrderItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author LGH
 * @date 2019-08-28 09:36:59
 */
@RestController
@RequestMapping("/seckill/seckillOrder")
@Tag(name = "平台端秒杀订单接口")
@AllArgsConstructor
public class SeckillOrderController {

    private final OrderItemService orderItemService;

    @GetMapping("/getSeckillIdByOrderNumber")
    @Operation(summary = "根据订单编号获取秒杀id")
    @Parameter(name = "orderNumber", description = "订单编号" , required = true)
    @PreAuthorize("@pms.hasPermission('seckill:seckillOrder:info')")
    public ServerResponseEntity<Long> getSeckillIdByOrderNumber(@RequestParam("orderNumber") String orderNumber) {
        // TODO stock
        OrderItem orderItem = orderItemService.getOne(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getOrderNumber, orderNumber));
        if (Objects.isNull(orderItem)) {
            throw new YamiShopBindException("yami.data.deleted.or.not.exist");
        }
        return ServerResponseEntity.success(orderItem.getActivityId());
    }
}
